/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.unilearn.subject;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author DidierLeung
 */
@Stateless
public class SubjectSessionBean implements SubjectSessionRemote
{
    @PersistenceContext
    private EntityManager em;
    
    public void addSubject(Subject s)
    {
        SubjectEntity aSubjectEntity = new SubjectEntity();
        aSubjectEntity.setSubjectEntityData(s);
        em.persist(aSubjectEntity);
    }

    public List<Subject> getAllSubjects()
    {
        List<Subject> results = new ArrayList<Subject>();
        Query allSubjectsQuery = em.createNamedQuery("findAllSubjects");
        List dbResults = allSubjectsQuery.getResultList();
        for (int i = 0; i < dbResults.size(); i++)
        {
            SubjectEntity aSubjectEntity = (SubjectEntity) dbResults.get(i);
            results.add(aSubjectEntity.getSubjectObject());
        }
        return results;
    }

    public Subject getSubject(String id)
    {
        SubjectEntity aSubjectEntity = em.find(SubjectEntity.class, id);
        return aSubjectEntity.getSubjectObject();
    }
    
    public Boolean isSubjectExist(String id)
    {
        SubjectEntity aSubjectEntity = em.find(SubjectEntity.class, id);
        return aSubjectEntity != null;
    }
}
